package com.platform.action;

import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;
import com.platform.model.User;
import com.platform.service.UserService;

public class UserAction implements Preparable {
	private UserService service;
	private List<User> users;
	private User user;
	private Long id;
	
	private String username;
	private String password;

	private static final String MAIN_JSP="/main.jsp";
	private static final String LOGIN_JSP="/login.jsp";
	
	private static final String QUERY_JSP="/page/UserPage/query_PtUser.jsp";
	private static final String LIST_JSP="/page/UserPage/list_PtUser.jsp";
	

	public UserAction(final UserService service) {
		this.service = service;
	}

	public String execute() {
		this.users = this.service.findAll();
		return Action.SUCCESS;
	}

	public String save() {
		this.service.save(this.user);
		this.user = new User();
		return this.execute();
	}

	public String remove() {
		this.service.remove(this.id);
		return this.execute();
	}

	public List<User> getUsers() {
		return this.users;
	}

	public String login(){
		if("1".equals(this.username)&&"1".equals(this.password))
			return MAIN_JSP;
		return LOGIN_JSP;
	}
	
	/**
	 * 查询
	 * @return
	 */
	public String query(){
		System.out.print("sss");
		return QUERY_JSP;
	}
	
	/**
	 * 列表
	 * @return
	 */
	public String list(){
		return LIST_JSP;
	}
	
	public Long getId() {
		return this.id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	public void prepare() throws Exception {
		if (this.id != null) {
			this.user = this.service.find(this.id);
		}
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(final User user) {
		this.user = user;
	}
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}